package de.msiggi.beans;

import java.rmi.RemoteException;
import java.util.Collections;
import java.util.Set;
import java.util.TreeMap;

import javax.xml.rpc.ServiceException;

import de.msiggi.Sportsdata.Testfiles.MatchDateComparator;
import de.msiggi.Sportsdata.Tools.GameDay;
import de.msiggi.Sportsdata.Tools.GenerateGameDays;
import de.msiggi.Sportsdata.Webservices.Group;
import de.msiggi.Sportsdata.Webservices.Matchdata;
import de.msiggi.Sportsdata.Webservices.SportsdataLocator;
import de.msiggi.Sportsdata.Webservices.SportsdataSoap;

public class CurrentGameDayBean {

	private GameDay currentGameDay;
	private SportsdataSoap sportsData;

	public CurrentGameDayBean() {
		try {
			sportsData = new SportsdataLocator().getSportsdataSoap();
			getCurrentGameDay();
		} catch (ServiceException e) {
			e.printStackTrace();
		}
	}

	public GameDay getCurrentGameDay() {
		Group currentGroup = null;
		try {
			currentGroup = sportsData.getCurrentGroup("BL1");
			if(currentGameDay != null){
				if(currentGameDay.getGamedayGroup().equals(currentGroup));
				return currentGameDay;
			}
		} catch (RemoteException e1) {
			e1.printStackTrace();
		}
		// Alle Spiele
		Matchdata[] matches;
		try {
			matches = sportsData.getMatchdataByLeagueSaison("BL1", "2013");
			
			GenerateGameDays gen = new GenerateGameDays();
			TreeMap<Integer, GameDay> gameDays = gen
					.generateSeasonWithGameDaySplit(matches);
			Set<Integer> keysc = gameDays.keySet();
			for (Integer integer : keysc) {
				if (integer == currentGroup.getGroupID()) {
					GameDay gd = gameDays.get(integer);
					currentGameDay = gd;
					Collections.sort(currentGameDay.getMatches(), new MatchDateComparator());
					return currentGameDay;
				} else {

				}
			}
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		System.err.println("damn");
		return currentGameDay;
	}

	public void setCurrentGameDay(GameDay currentGameDay) {
		this.currentGameDay = currentGameDay;
	}

	public static void main(String[] args) {
		new CurrentGameDayBean().getCurrentGameDay();
	}

}
